package net.lab1024.sa.admin.module.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfCategory;
import net.lab1024.sa.admin.module.workflow.domain.form.WfCategoryBo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfCategoryVo;
import net.lab1024.sa.admin.module.workflow.service.WfCategoryService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.module.support.repeatsubmit.annoation.RepeatSubmit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 流程分类Controller
 *
 * @author KonBAI
 * @createTime 2022/3/10 00:12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/category")
@Slf4j
public class WfCategoryController {

    @Resource
    private WfCategoryService wfCategoryService;

    /**
     * 查询流程分类列表
     */
    @SaCheckPermission("workflow:category:list")
    @GetMapping("/list")

    public ResponseDTO<PageResult<WfCategoryVo>> list(WfCategoryBo category) {
        PageResult<WfCategoryVo> page = wfCategoryService.queryPageList(category);
        return ResponseDTO.ok(page);
    }

    /**
     * 查询全部的流程分类列表
     */
    @SaCheckLogin
    @GetMapping("/listAll")
    public ResponseDTO<List<WfCategoryVo>> listAll(WfCategoryBo category) {
        log.info("查询全部的流程分类列表请求参数:{}", JSON.toJSONString(category));
        List<WfCategoryVo> list = wfCategoryService.queryList(category);
        return ResponseDTO.ok(list);
    }

    /**
     * 导出流程分类列表
     */
    @SaCheckPermission("workflow:category:export")
    @PostMapping("/export")
    public void export(@Validated WfCategoryBo category, HttpServletResponse response) {
        List<WfCategoryVo> list = wfCategoryService.queryList(category);
        // ExcelUtil.exportExcel(list, "流程分类", WfCategoryVo.class, response);
    }

    /**
     * 获取流程分类详细信息
     *
     * @param categoryId 分类主键
     */
    @SaCheckPermission("workflow:category:query")
    @GetMapping("/{categoryId}")
    public ResponseDTO<WfCategory> getInfo(@NotNull(message = "主键不能为空") @PathVariable("categoryId") Long categoryId) {
        WfCategory wfCategory = wfCategoryService.queryById(categoryId);
        return ResponseDTO.ok(wfCategory);
    }

    /**
     * 新增流程分类
     */
    @SaCheckPermission("workflow:category:add")
    @RepeatSubmit()
    @PostMapping()
    public ResponseDTO<String> add(@Validated @RequestBody WfCategory category) {
        if (!wfCategoryService.checkCategoryCodeUnique(category)) {
            return ResponseDTO.ok("新增流程分类'" + category.getCategoryName() + "'失败，流程编码已存在");
        }
        wfCategoryService.insertCategory(category);
        return ResponseDTO.ok();
    }

    /**
     * 修改流程分类
     */
    @RepeatSubmit()
    @PutMapping()
    public ResponseDTO<String> edit(@Validated @RequestBody WfCategory category) {
        if (!wfCategoryService.checkCategoryCodeUnique(category)) {
            return ResponseDTO.ok("修改流程分类'" + category.getCategoryName() + "'失败，流程编码已存在");
        }
        wfCategoryService.updateCategory(category);
        return ResponseDTO.ok("修改流程分类'" + category.getCategoryName() + "'失败，流程编码已存在");
    }

    /**
     * 删除流程分类
     *
     * @param categoryIds 分类主键串
     */
    @SaCheckPermission("workflow:category:remove")
    @DeleteMapping("/{categoryIds}")
    public ResponseDTO<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] categoryIds) {
        wfCategoryService.deleteWithValidByIds(Arrays.asList(categoryIds), true);

        return ResponseDTO.ok();
    }
}
